package com.woniu.listener;

import com.rabbitmq.client.Channel;
import com.woniu.outlet.dao.ReliablePoMapper;
import com.woniu.outlet.po.ReliablePo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 订单消息监听类
 */
@Component
public class OrderMessageListener {

    @Autowired
    private ReliablePoMapper reliablePoMapper;

//    @RabbitListener(queues = "order_direct_queue")
    public void getMessage(Message message, Channel channel){
        //消息去重
        String messageId = message.getMessageProperties().getClusterId();
        ReliablePo reliablePo = new ReliablePo();
        reliablePo.setMessageid(Integer.parseInt(messageId));
        try {
            reliablePoMapper.insert(reliablePo);
        } catch (Exception e) {
            e.printStackTrace();
            //拒绝应答
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
////        //执行业务流程
//        String orderId = new String(message.getBody());
//        //
    }
}
